# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_forecast_information_impl.py
# @Desc    :
from app import db
from infrastructure.po.mode.jasco_order import JascoOrder
from infrastructure.po.mode.jasco_forecast_information import JascoForecastInformation
from sqlalchemy import and_, or_, desc, asc


class JascoForecastInformationImpl(object):

    @classmethod
    def get_forecast_information_list(cls, order_id, page=1, page_size=999):
        query = db.session.query(JascoForecastInformation).filter(JascoForecastInformation.order_id == order_id)
        return query.order_by(JascoForecastInformation.create_time.desc()).paginate(page=page, per_page=page_size), query.count()

    @classmethod
    def add_forecast_information(cls, order_id,
            cargo_code,
            length,
            width,
            height,
            gross_weight,
            volume):
        count = (
            db.session.query(JascoForecastInformation).filter(and_(JascoForecastInformation.cargo_code == cargo_code, JascoForecastInformation.order_id == order_id)).count()
        )
        if count > 0:
            return False
        else:
            jasco_forecast_information = JascoForecastInformation(
                cargo_code=cargo_code,
                length=length,
                width=width,
                height=height,
                gross_weight=gross_weight,
                volume=volume,
                order_id=order_id
            )
            db.session.add(jasco_forecast_information)
            db.session.flush()
            db.session.commit()
            return True

    @classmethod
    def edit_forecast_information(cls, id,
                                  order_id,
                                cargo_code,
                                length,
                                width,
                                height,
                                gross_weight,
                                volume):
        query = db.session.query(JascoForecastInformation).filter(and_(JascoForecastInformation.id == id, JascoForecastInformation.order_id == order_id))
        if query.count() == 0:
            return False
        else:
            jasco_forecast_information = query.first()
            jasco_forecast_information.cargo_code = cargo_code
            jasco_forecast_information.length = length
            jasco_forecast_information.width = width
            jasco_forecast_information.height = height
            jasco_forecast_information.gross_weight = gross_weight
            jasco_forecast_information.volume = volume
            db.session.flush()
            db.session.commit()
            return True

    @classmethod
    def delete_forecast_information(cls, id, order_id):
        query = db.session.query(JascoForecastInformation).filter(and_(JascoForecastInformation.id == id, JascoForecastInformation.order_id == order_id))
        if query.count() == 0:
            return False
        else:
            query.delete()
            db.session.flush()
            db.session.commit()
            return True

